package com.mikelouis.test.scala.proginscala.chapter29

/**
 * Accessing wildcard types from Scala
 * refer to Wild.java
 * Created by yuazhu on 2/26/2015.
 */
object UseWild {

  import scala.collection.mutable.Set
  import java.util.Collection

  abstract class SetAndType {
    type Elem
    val set: Set[Elem]
  }

  def javaSet2ScalaSet[T](jset: Collection[T]): SetAndType = {
    val sset = Set.empty[T] // now T can be named!

    val iter = jset.iterator
    while (iter.hasNext)
      sset += iter.next()

    new SetAndType {
      type Elem = T
      val set = sset
    }
  }


  def main(args: Array[String]) {
    val content = (new Wild).contents
    println(content.getClass + ": " + content)

    val setAndType = javaSet2ScalaSet(content)
    val set: Set[setAndType.Elem] =
      setAndType.set

    println(set.getClass + ": " + set)
  }
}
